package leetcode;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2022-01-23 17:37
 **/
public class LeetCode5972 {

    public int numberOfArrays(int[] differences, int lower, int upper) {
        int n = differences.length;
        for (int i = lower; i <= upper; ++i) {
            int cur = i;
            int min = i;
            int max = i;
            for (int j = 0; j < n; ++j) {
                cur += differences[j];
                if (cur < lower || cur > upper) {
                    break;
                }

                min = Math.min(min, cur);
                max = Math.max(max, cur);
                if (j == n - 1) {
                    return upper - max + min - lower + 1;
                }
            }
        }
        return 0;
    }

}
